/**
 * Created by admin on 2017/9/28.
 */
define([
    "helpers/network",
    "libs/md5"
], function (Network, md5) {

    var onLoginClick = function () {
        var form = $$("form").getValues();
        if (!form) {
            webix.message({type: "error", text: "请填写完整的账号密码！"});
            return
        }
        var obj = {
            username: form.username,
            password: md5(form.password)
        };
        Network.ajax("/admin.login.do", obj, function (ret) {
            sessionStorage.setItem("userDisplayName", ret.data.username);
            sessionStorage.setItem("username", ret.data.username);
            sessionStorage.setItem("userid", ret.data.id);
            if (!ret.data.id) {
                webix.message("用户角色异常");
                return
            }
            _.forEach(ret.data.authority, function (each) {
                sessionStorage.setItem(each.authority, md5(each))
            })
            // var obj = {
            //     params: {
            //         id:ret.data.data.role.id,
            //         type:"Role"
            //     }
            // };
            // Network.ajax("/BizView.queryOne.query", obj, function (ret) {
            //
            //     var authorities = webix.ui({
            //         view: "tree",
            //         data: ret.data.data.authorities || []
            //     });
            //     authorities.data.eachLeaf(0, function (eachLeaf) {
            //         var value = "Role_" + eachLeaf.id;
            //         if (eachLeaf.checked) sessionStorage.setItem(value, md5(value))
            //     });
            //     authorities.destructor();
            window.location.href = ''
            // })
        })
    };

    var layout = {
        view: "form", id: "form", scroll: false, width: 400, complexData: true, css: {"margin": "10% auto 0"},
        rows: [
            {
                view: "text", label: '用户名', name: "username", required: true, value:"admin",
                on: {
                    onKeyPress: function (code) {
                        if (code == 13) onLoginClick();
                    }
                }
            },
            {
                view: "text", type: "password", label: '密码', name: "password", required: true,value:"admin",
                on: {
                    onKeyPress: function (code) {
                        if (code == 13) onLoginClick();
                    }
                }
            },
            {
                view: "button", value: "登录",
                click: function () {
                    onLoginClick()
                }
            }
        ]
    };

    return {
        $ui: layout
    };

});